<template>
	<view class="tbody">
		<text class="name">活动名称</text>
		<input type="text" name="" id="" class="headline"   v-model="formData.title">
		<text class="name">封面图片</text>
		<view class="cover">
			<u-upload :fileList="fileList4" @afterRead="afterRead" @delete="deletePic" name="4" multiple
				:maxCount="1"></u-upload>
		</view>
		<text class="name">活动时间</text>
		<view class="headline" @click="show = !show">
			{{time}}
			<u-calendar startText=" " endText=" " :show="show" :mode="mode" @confirm="confirm"
				color="#16c1db"></u-calendar>
		</view>
		<text class="name">参与人数上限</text>
		<input type="text" name="" id="" class="headline"   v-model="formData.totalNum">
		<text class="name">活动介绍</text>
		<!-- 活动介绍 -->
		<view class="editor" style="padding: 0;">
			<!-- 编辑内容 -->
				<cu-editor showImgSize="true" class="editor-one" ref="editor" :url="uploadUrl" :header="header" :formData="formData.introduce"
					@before="onUploadBefore" :value="content" @save="save"></cu-editor>	
		</view>
		<text class="name">活动安排</text>
		<!-- 活动介绍 -->
		<view class="box">
			<editor  @blur="blurtwo"></editor>
		</view>
		<!--确认发布按钮 -->
		<view class="issue" @click="uploading">
			确认发布
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				ip: this.$store.state.user.URL,
				value1: '',//活动介绍
				value2:'',//活动安排
				time: '', //发布活动时间
				show: false, //控制日历弹出
				mode: 'range', //可以选择范围日期
				fileList4: [
				],
				// 需要上传的数据
				formData:{
					title:'',//活动名称
					coverImage:'',//封面图片,
					startTime:'',//活动开始时间,
					endTime:'',//活动结束时间,
					totalNum:'',//人数上限,
					introduce:'',//活动介绍,
					particulars:''//活动安排
					
				}
			};
		},
		methods: {
			// 组件通讯，子传父，拿到文本框里面的内容，然后赋值给定义好的变量
			save(res){
				this.formData.introduce = res.html
			},
			// 点击上传
			 uploading(){
				 // 调用组件editor上面方法save获取编辑器内容，然后save会通过子传父的方法把文本框内容传过来
				 this.$refs.editor.save()
				 
				   
				
				
				 // 用定时器调用接口
				setTimeout(async()=>{
					this.formData.introduce = this.formData.introduce.replace(/src="https:\/\/www\.milefuwu\.xyz:441\//g, 'src="/');
					// console.log(this.formData.introduce);
					// return
					// 遍历数据，不能为空，有一项为空就return出去，不允许发布
					for (let key in this.formData) {
					 if(!this.formData[key]){
						 uni.showToast({
						 	icon:'error',
							title:'上传活动不为空'
						 })
						 return
					 }
					}
					// 通过之后上传
					const res = await uni.$http.put('/business/content',this.formData)
					if(res.data.code === 200){
						uni.showToast({
							icon:'success',
							title:'上传成功'
						})
					}else{
						uni.showToast({
							icon:'error',
							title:'上传失败'
						})
					}
				},1000)
			},
			blur(html, text, delta) {
				this.formData.introduce = html.detail.html
			},
			blurtwo(html, text, delta) {
				this.formData.particulars = html.detail.html
			},
			// 删除图片
			deletePic(event) {
				this[`fileList${event.name}`].splice(event.index, 1)
				this.formData.coverImage.splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				// 当设置 multiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this[`fileList${event.name}`].length
				lists.map((item) => {
					this[`fileList${event.name}`].push({
						...item,
						// status: 'uploading',
						// message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					let item = this[`fileList${event.name}`][fileListLen]
					this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
			},
			uploadFilePromise(pathurl) {
				// 上传封面图片的路径
				// console.log(pathurl);
				return new Promise((resolve, reject) => {
					const  URL = this.$store.state.user.URL
					let a = uni.uploadFile({
						url: `${URL}/appCommon/upload`, // 仅为示例，非真实的接口地址
						filePath: pathurl,
						name: 'file',
						formData: {
							user: 'test'
						},
						success: (res) => {
							setTimeout(() => {
								console.log(res);
								this.formData.coverImage = res.data
							}, 1000)
						}
					});
				})
			},
			// 点击确定选择活动时间 
			confirm(e) {
				console.log(e);
				const startTime = e[0]
				const endTime = e.at(-1)
				this.time = `${startTime} ~ ${endTime}`
				// this.time.push(endTime)
				this.formData.startTime = startTime
				this.formData.endTime = endTime
				this.show = false
			}
		}
	}
</script>

<style lang="scss">
	::v-deep .fixed-top.data-v-7b039d96{
		height: 0 !important;
	}
	.editor-one {
		height: 300rpx !important;
	}
	::v-deep .fixed-top {
		display: none;
	}
	
	::v-deep .fixed-top__place {
		display: none;
	}
	.editor {
		margin-top: 24rpx;
		min-height: 200rpx;
		opacity: 1;
		border-radius: 20rpx;
		background: #F5FDFF;
		box-shadow: 0px 0px 2rpx 0px #16C1DB;
		margin-bottom: 88rpx;
	}
	.box{
		margin-top: 48rpx;
		padding: 0 32rpx;
		height: 500rpx;
		border-radius: 20rpx;
		background: #F2FDFF;
		box-shadow: 0px 0px 2rpx 0px #16C1DB;
		padding-top: 32rpx;
		margin-bottom: 144rpx;
		font-size: 24rpx;
	}
	.name {
		opacity: 0.5;
		font-size: 24rpx;
		font-weight: 400;
		line-height: 0px;
		color: rgba(20, 58, 64, 1);
	}
	.editor-one {
		height: 300rpx !important;
	}
	.headline {
		margin-top: 24rpx;
		margin-bottom: 64rpx;
		padding-left: 24rpx;
		box-sizing: border-box;
		height: 78rpx;
		opacity: 1;
		border-radius: 20rpx;
		background: #F5FDFF;
		box-shadow: 0px 0px 2rpx 0px #16C1DB;
		font-size: 24rpx;
		font-weight: 400;
		line-height: 0px;
		color: rgba(20, 58, 64, 1);
		line-height: 74rpx;
	}

	.cover {
		border-radius: 20rpx;
		background: #F5FDFF;
		box-shadow: 0px 0px 2rpx 0px #16C1DB;
		padding: 32rpx 0 32rpx 32rpx;
		margin-top: 24rpx;
		margin-bottom: 64rpx;
	}

	::v-deep .u-textarea {
		box-sizing: border-box;
		margin-top: 48rpx;
		min-height: 204rpx;
		border-radius: 20rpx !important;
		background: #F5FDFF !important;
		border: none !important;
		box-shadow: 0px 0px 2rpx 0px #16C1DB !important;
		padding: 24rpx;
		padding-bottom: 256rpx;
		margin-bottom: 64rpx;
	}

	::v-deep .u-textarea .data-v-09988a29 {
		background: #F5FDFF;
	}
	.issue{
		height: 88rpx;
		border-radius: 60rpx;
		background: #16C1DB;
		
		font-size: 32rpx;
		font-weight: 400;
		color: rgba(235, 252, 255, 1);
		text-align: center;
		line-height: 88rpx;
	}
</style>